package com.yunshipei.ui.fragment;


import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.webkit.ValueCallback;

import com.yunshipei.common.Globals;
import com.yunshipei.common.wedigt.YspTitleBar;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.YspEvent;
import com.yunshipei.utils.BaseUtil;

import org.xwalk.core.XWalkResourceClient;
import org.xwalk.core.XWalkView;
import org.xwalk.core.XWalkWebResourceRequest;
import org.xwalk.core.XWalkWebResourceResponse;

import java.io.InputStream;

import butterknife.Bind;
import io.rong.eventbus.EventBus;

public class FontSizeSettingFragment extends BaseBodyFragment implements TextSizeSettingFragment.OnFragmentInteractionListener {

    public static FontSizeSettingFragment newInstance() {
        return new FontSizeSettingFragment();
    }

    @Bind(R.id.xwal_view_font_zize_demo)
    protected XWalkView xWalkView;
    @Bind(R.id.title_bar_font_settings)
    protected YspTitleBar titleBar;

    @Override
    public int viewLayout() {
        return R.layout.fragment_font_size_setting;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isPad()) {
            titleBar.setBackClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EventBus.getDefault().post(new YspEvent.PadClick(SettingsFragment.class.getName()));
                }
            });
        }
        xWalkView.setResourceClient(new XWalkResourceClient(xWalkView) {
            @Override
            public void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback, SslError error) {
                callback.onReceiveValue(true);
            }

            @Override
            public XWalkWebResourceResponse shouldInterceptLoadRequest(XWalkView view, XWalkWebResourceRequest request) {
                String url = request.getUrl().toString();
                Uri uri = Uri.parse(url);
                if (uri != null) {
                    String encodePath = uri.getEncodedPath();
                    if (!TextUtils.isEmpty(encodePath) && encodePath.endsWith("_init.js")) {
                        try {
                            InputStream is = view.getContext().getAssets().open("empty");
                            return super.createXWalkWebResourceResponse("text/javascript", "UTF-8", is);
                        } catch (Exception ignored) {
                        }
                    }
                }
                return super.shouldInterceptLoadRequest(view, request);
            }
        });
        int position = YspPreferences.getInstance().getSharedPreferences().getInt(Globals.WEB_VIEW_TEXT_ZOOM, 1);
        Fragment fragment = TextSizeSettingFragment.newInstance(position);
        getChildFragmentManager().beginTransaction().replace(R.id.fl_font_size_settings, fragment, fragment.getClass().getName()).commit();
        xWalkView.getSettings().setTextZoom(BaseUtil.parseTextZoomSize(position));
        xWalkView.loadUrl("file:///android_asset/font_example/font.html");
    }

    @Override
    public void changeWebSize(int position) {
        xWalkView.getSettings().setTextZoom(BaseUtil.parseTextZoomSize(position));
        EventBus.getDefault().post(new YspEvent.BrowserFontSizeChange(position));
    }

    @Override
    public void onDestroyView() {
        xWalkView.onDestroy();
        super.onDestroyView();
    }
}
